package com.shuorikeji.huohuo.ad;

import android.content.Context;
import android.util.Log;

import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.common.Constant;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.qq.e.ads.rewardvideo.RewardVideoAD;
import com.qq.e.ads.rewardvideo.RewardVideoADListener;
import com.qq.e.comm.util.AdError;

import java.util.Map;

public class TxJlAdHelper {
    private RewardVideoAD rewardVideoAD;
    private Context context;
    private boolean adLoaded = false;
    private ClickCallBack callBack;

    public TxJlAdHelper(Context context, ClickCallBack callBack) {
        this.context = context;
        this.callBack = callBack;
        initAd();
    }

    private void initAd() {
        rewardVideoAD = new RewardVideoAD(context, Constant.TX_YLH_AD_ID, new RewardVideoADListener() {
            @Override
            public void onADLoad() {
                adLoaded = true;
                if (rewardVideoAD.isValid()) {
                    showAd();
                } else {
                    callBack.onClick(2,null);
                    ToastShowUtil.showToastCenter(context, "广告无效");
                }
            }

            @Override
            public void onVideoCached() {

            }

            @Override
            public void onADShow() {
                callBack.onClick(2,null);
            }

            @Override
            public void onADExpose() {

            }

            @Override
            public void onReward(Map<String, Object> map) {
                callBack.onClick(1, rewardVideoAD.getECPM());
            }

            @Override
            public void onADClick() {
                callBack.onClick(2,null);
            }

            @Override
            public void onVideoComplete() {
                callBack.onClick(2,null);
            }

            @Override
            public void onADClose() {
                callBack.onClick(2,null);
                Log.d("wxl", "腾讯激励视频广告被关闭");
            }

            @Override
            public void onError(AdError adError) {
                callBack.onClick(2,null);
                ToastShowUtil.showToastCenter(context, "腾讯广告加载失败：" + adError.getErrorCode() + "：" + adError.getErrorMsg());
            }
        }, true);
        // 加载广告
        loadAd();
    }

    private void loadAd() {
        adLoaded = false;
        rewardVideoAD.loadAD();
    }

    public void showAd() {
        if (adLoaded) {
            rewardVideoAD.showAD();
        } else {
            loadAd();
        }
    }
}
